<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
            <el-input v-model="form.base_currency" placeholder="基础币种" />
        </el-form-item>
        <el-form-item>
            <el-input v-model="form.quote_currency" placeholder="目标币种" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" width="80" />
          <el-table-column prop="date_at" label="日期" align="center" width="100" />
          <el-table-column label="基准币种" align="center">
            <template #default="scope">
                {{ scope.row.base_name }}/{{ scope.row.base_currency }}
            </template>
          </el-table-column>
          <el-table-column label="目标币种" align="center">
            <template #default="scope">
                {{ scope.row.quote_name }}/{{ scope.row.quote_currency }}
            </template>
          </el-table-column>
          <el-table-column label="现汇买入价" align="center">
            <template #default="scope">
                {{ scope.row.se_bid == 0 ? '-' : scope.row.se_bid}}
            </template>
          </el-table-column>
          <el-table-column label="现汇卖出价" align="center">
            <template #default="scope">
                {{ scope.row.se_ask == 0 ? '-' : scope.row.se_ask}}
            </template>
          </el-table-column>
          <el-table-column label="现钞买入价" align="center">
          <template #default="scope">
                {{ scope.row.bn_bid == 0 ? '-' : scope.row.bn_bid}}
            </template>
          </el-table-column>
          <el-table-column label="现钞卖出价" align="center">
          <template #default="scope">
                {{ scope.row.bn_ask == 0 ? '-' : scope.row.bn_ask}}
            </template>
          </el-table-column>
          <el-table-column label="中行折算价" align="center">
          <template #default="scope">
                {{ scope.row.boc_conv == 0 ? '-' : scope.row.boc_conv}}
            </template>
          </el-table-column>
          <!--<el-table-column prop="ratio" label="浮动比例" align="center" />-->
          <el-table-column prop="created_at" label="发布时间" align="center" width="180" />
          <el-table-column prop="source" label="数据来源" align="center" width="80" />
          <!--<el-table-column prop="price" label="价格" align="center" />-->
          <!--<el-table-column prop="ratio" label="浮动比例" align="center" />-->
          <!--<el-table-column prop="initial_price" label="初始价格" align="center" />-->
          <el-table-column prop="createtime" label="采集时间" align="center" :formatter="formatterTime" width="180" />
          <el-table-column label="操作" align="center" fixed="right" width="150">
            <template #default="scope">
                <el-button type="primary" plain
                @click="handleOperate(scope.$index, scope.row, 'set')">配置商户价格</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm @cancel="cancel" @closeDialog="closeDialog" @submitForm="submit"
        :dialogVisible="dialogVisible" :dialogForm="dialogForm" :btnLoading="btnLoading" :dialogTitle="dialogTitle"
        :merchantList="merchantList"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, timestamp } from "@/utils/utils";
import { exchangeBaseRateApi, exchangeMerchantRateSetApi } from "@/api/payServerse/exchange";
import { merchantSelectApi } from "@/api/payServerse/merchant";
import DialogForm from "./components/dialogForm.vue";

const dialogVisible = ref(false);
const dialogTitle = ref("");
const btnLoading = ref(false);
const btnType = ref(null);
const loading = ref(false);
const merchantList = ref([]);
const tableData = ref([]);

const formData = {
    base_currency: "",
    quote_currency: "",
    merchant_id: "",
};
const form = reactive({ ...formData });
// 新增、编辑参数
const dialogFormData = {
    merchantChecked: [],
    boc_select: "",
    price: "",
    expire_time: ""
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
});

// 参数
const param = () => {
    return {
        page: pageable.pageNum,
        pagesize: pageable.pageSize,
        merchant_id: form.merchant_id,
        base_currency: form.base_currency,
        quote_currency: form.quote_currency,
    };
};

// 行操作
const handleOperate = async (index, row, type) => {
    // 编辑
    if (type === "set") {
        btnType.value = "set";
        Object.assign(dialogForm, { ...row,
            merchantChecked: [],
        });
        dialogVisible.value = true;
        dialogTitle.value = '配置商户币种汇率';
        const mer = await merchantSelectApi(param())
        merchantList.value = mer.data
    }
};
// 提交
const submit = async param => {
    btnLoading.value = true;
    if (btnType.value == "set") {
        const res = await exchangeMerchantRateSetApi(param);
        ElMessage({
            message: res.message,
            type: "success"
        });
        dialogVisible.value = false;
    }
    btnLoading.value = false;
    getTbaleData();
};

// 获取表格数据
const getTbaleData = async () => {
    loading.value = true;
    try {
        const res = await exchangeBaseRateApi(param());
        loading.value = false;
        tableData.value = res.data.results;
        pageable.total = res.data.count;
    } catch (error) {
        loading.value = false;
    }
};
//关闭弹窗
const closeDialog = () => {
    dialogVisible.value = false;
    btnType.value = null;
    Object.assign(dialogForm, { ...dialogFormData });
};
// 取消
const cancel = () => {
    dialogVisible.value = false;
    btnType.value = null;
    Object.assign(dialogForm, { ...dialogFormData });
};
// 重置
const onReset = () => {
    Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
    getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
    pageable.pageSize = number;
    getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
    pageable.pageNum = number;
    getTbaleData();
};
onMounted(() => {
    getTbaleData();
});
</script>
